<?php

namespace App\Admin\Controllers\order;

use App\Admin\Extensions\Filter\TimestampBetween;
use App\Models\Order\Info;
use App\Models\Order\Subscribe;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;

class SubscribeController extends Controller
{
    use HasResourceActions;
    public $header = '家长订单列表';

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Subscribe::class, function (Grid $grid) {

            $grid->model()->with('user')->with('order')->orderBy('id','desc');
            $grid->id('ID')->sortable();
            $grid->user()->id('用户id');
            $grid->user()->username('用户名');
            $grid->user()->telephone('手机号');
            $grid->order()->id('订单ID');
            $grid->order()->order_sn('订单编号');
            $grid->monthNum('购买类型');
            $grid->order()->order_amount('订单金额');
            $grid->order()->create_time('创建时间')->display(function ($create_time){
                if (!$create_time) return '';
                return date('Y-m-d H:i:s', $create_time);
            });
            $grid->order()->pay_status('订单状态')->using(Info::$pay_status);
            $grid->order()->pay_time('支付时间')->display(function ($pay_time){
                if (!$pay_time) return '';
                return date('Y-m-d H:i:s', $pay_time);
            });
            $grid->order()->mobile_type('手机类型')->using(Info::$mobile_type);

            $grid->disableActions();
            $grid->disableCreateButton();
            $grid->filter(function (Grid\Filter $filter){

                $filter->disableIdFilter();
                $filter->like('order.order_sn', '订单编号');
                $filter->equal('order.pay_status', '订单状态')->select(Info::$pay_status);
                $filter->use(new TimestampBetween('order.pay_time', '支付时间'))->datetime();
                $filter->equal('order.mobile_type', '手机类型')->select(Info::$mobile_type);

                // 快捷筛选
                /*$filter->scope('pay_status')->whereHas('order', function ($query) {
                    $query->where('pay_status', 1);
                });*/
            });

        });
    }

}
